package Juego;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;

import Archivos.Escritor;
import Archivos.Lector;
import Disparo.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;

import javax.swing.SwingUtilities;
import Inteligencia.*;
import Naves.NaveCaza;
import Naves.NaveXwing;
import Naves.NavesEnemiga;
import Naves.NavesUsuario;

import java.awt.Color;
import java.awt.Point;
import TDALista.*;
import Naves.*;
import PowerUps.PowerUpGenerico;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

import PowerUps.*;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/

public class  Gui extends javax.swing.JFrame{
	private JTextField puntaje;
	private String playerName;
	private JPanel PanelPrincipal;
	private JButton BotonSalir;
	private JLabel PuntosJugador;
	private JLabel FullPower;
	private JLabel PowerVida;
	private JLabel BombasPower;
	private JLabel DisparoPower;
	private JLabel PowerUP4;
	private JLabel PowerUP3;
	private JLabel PowerUP2;
	private JLabel PowerUP1;
	private JLabel Nave1a;
	private JLabel Nave2a;
	private JLabel Nave3a;
	private JPanel PanelSecundario;
	private JLabel LabelNaves;
	private ImageIcon navehalcon = new ImageIcon(getClass().getClassLoader().getResource("imagenes/navehalcon.png"));
	private ImageIcon navexwing = new ImageIcon(getClass().getClassLoader().getResource("imagenes/xwing.png"));
	private ImageIcon bombasresto = new ImageIcon(getClass().getClassLoader().getResource("imagenes/bomba.png"));
	private JLabel ImagenchiquitaHalcon;
	private JLabel ImagenchiquitaXwing;
	private ImageIcon powervida = new ImageIcon(getClass().getClassLoader().getResource("imagenes/vida.png"));
	private JLabel TextoNaveHalcon;
	private JLabel TextoNaveXwing;
	private JButton BotonNaveHalcon;
	private JButton BotonNaveXwing;
	private JLabel LabelVida;
	private ImageIcon powerfull = new ImageIcon(getClass().getClassLoader().getResource("imagenes/recarga.png"));
	private ImageIcon powerbomb = new ImageIcon(getClass().getClassLoader().getResource("imagenes/bombas.png"));
	private ImageIcon powerarma = new ImageIcon(getClass().getClassLoader().getResource("imagenes/disparos.png"));
	private ImageIcon espacio = new ImageIcon(getClass().getClassLoader().getResource("imagenes/espacio.gif"));
	private ImageIcon espacio2 = new ImageIcon(getClass().getClassLoader().getResource("imagenes/espacio2.png"));
	private ImageIcon botoncerrar = new ImageIcon(getClass().getClassLoader().getResource("imagenes/botonsalir.png"));
	
	private NavesUsuario jugador;
	protected PositionList<PowerUpGenerico> powerups;
	protected PositionList<NavesEnemiga> listadeenemigos;
	protected PositionList<Bala> balasenemigas;
	protected PositionList<Bala> balasamigas;
	protected enemigosAI inteligenciaenemigos;
	protected balasAI inteligenciabalas;
	protected powerAI inteligenciapowerup;
	protected int ContadorPuntaje=0;
	private JLabel LabelFinal;
	private Thread audio;
	private JLabel Bomba1a;
	private JLabel Bomba2a;
	private JLabel Bomba3a;
	private JLabel fondo1;
	private JLabel fondo2;
	private JLabel Fondo3;
	private ImageIcon navesvidasimagenes;

	
	
	
	
	/**
	* Auto-generated main method to display this JFrame
	*/
	
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Gui inst = new Gui();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public Gui() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			pack();
			
				
			this.setSize(800, 600);
			this.setResizable(false);
			this.setTitle("Star Wars");
			getContentPane().setLayout(null);
			
			{
				PanelPrincipal = new JPanel();
				getContentPane().add(PanelPrincipal);
				PanelPrincipal.setLayout(null);
				PanelPrincipal.setBounds(0, 0, 800, 500);
				PanelPrincipal.setBackground(new java.awt.Color(0,0,0));
				
			}
			{
				LabelNaves = new JLabel();
				PanelPrincipal.add(LabelNaves);
				LabelNaves.setBounds(0, 0, 800, 500);
			}
			{
				PanelSecundario = new JPanel();
				getContentPane().add(PanelSecundario);
				PanelSecundario.setBounds(0, 500, 800, 88);
				PanelSecundario.setLayout(null);
				PanelSecundario.setBackground(new java.awt.Color(0,0,0));
				{
					BotonNaveXwing = new JButton();
					PanelSecundario.add(BotonNaveXwing);
					BotonNaveXwing.setText("Nave - Xwing");
					BotonNaveXwing.setBounds(14, 6, 111, 23);
					BotonNaveXwing.setFocusable(false);
					BotonNaveXwing.setForeground(new java.awt.Color(255,0,0));
					BotonNaveXwing.setBackground(new java.awt.Color(0,0,0));
					BotonNaveXwing.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
					OyenteXwing oyentexwing=new OyenteXwing();
					BotonNaveXwing.addActionListener(oyentexwing);
				}
				{
					BotonNaveHalcon = new JButton();
					PanelSecundario.add(BotonNaveHalcon);
					BotonNaveHalcon.setText("Nave - Halcon");
					BotonNaveHalcon.setBounds(14, 43, 111, 23);
					BotonNaveHalcon.setFocusable(false);
					BotonNaveHalcon.setForeground(new java.awt.Color(255,0,0));
					BotonNaveHalcon.setBackground(new java.awt.Color(0,0,0));
					BotonNaveHalcon.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
					OyenteHalcon oyenteHalcon=new OyenteHalcon();
					BotonNaveHalcon.addActionListener(oyenteHalcon);
				}
				
				{
					TextoNaveXwing = new JLabel();
					PanelSecundario.add(TextoNaveXwing);
					TextoNaveXwing.setText("     Vida: 500  Velocidad: 10");
					TextoNaveXwing.setBounds(139, 13, 284, 16);
					TextoNaveXwing.setForeground(new java.awt.Color(255,0,0));
					TextoNaveXwing.setFont(new java.awt.Font("Segoe UI",1,13));
					
				}
				{
					TextoNaveHalcon = new JLabel();
					PanelSecundario.add(TextoNaveHalcon);
					TextoNaveHalcon.setText("     Vida: 1000 Velocidad: 7");
					TextoNaveHalcon.setBounds(139, 45, 284, 16);
					TextoNaveHalcon.setForeground(new java.awt.Color(255,0,0));
					TextoNaveHalcon.setFont(new java.awt.Font("Segoe UI",1,13));
				}
				{
					ImagenchiquitaXwing = new JLabel();
					PanelSecundario.add(ImagenchiquitaXwing);
					ImagenchiquitaXwing.setIcon(navexwing);
					ImagenchiquitaXwing.setBounds(125, 8, 30, 30);
					ImagenchiquitaXwing.setVisible(true);
				}
				{
					ImagenchiquitaHalcon = new JLabel();
					PanelSecundario.add(ImagenchiquitaHalcon);
					ImagenchiquitaHalcon.setIcon(navehalcon);
					ImagenchiquitaHalcon.setBounds(125, 40, 30, 30);
					ImagenchiquitaHalcon.setVisible(true);

				}
				

				{
					
					Nave1a = new JLabel(navexwing);
					PanelSecundario.add(Nave1a);
					Nave1a.setVisible(true);
					Nave1a.setBounds(5, 15, 52, 57);
				}
				{
					Nave2a = new JLabel(navexwing);
					PanelSecundario.add(Nave2a);
					Nave2a.setVisible(true);
					Nave2a.setBounds(56, 15, 52, 57);
				}
				{
					Nave3a = new JLabel(navexwing);
					PanelSecundario.add(Nave3a);
					Nave3a.setVisible(true);
					Nave3a.setBounds(108, 15, 52, 57);
				}
				{
					Bomba1a = new JLabel(bombasresto);
					PanelSecundario.add(Bomba1a);
					Bomba1a.setVisible(true);
					Bomba1a.setBounds(200, 15, 52, 57);
				}
				{
					Bomba2a = new JLabel(bombasresto);
					PanelSecundario.add(Bomba2a);
					Bomba2a.setVisible(true);
					Bomba2a.setBounds(252, 15, 52, 57);
				}
				{
					Bomba3a = new JLabel(bombasresto);
					PanelSecundario.add(Bomba3a);
					Bomba3a.setVisible(true);
					Bomba3a.setBounds(304, 15, 52, 57);
				}
				{
					PowerUP1 = new JLabel();
					PanelSecundario.add(PowerUP1);
					PowerUP1.setText("Vida Extra.");
					PowerUP1.setBounds(527, 17, 89, 16);
					PowerUP1.setForeground(new java.awt.Color(255,0,0));
				}
				{
					PowerUP2 = new JLabel();
					PanelSecundario.add(PowerUP2);
					PowerUP2.setText("Full Vida.");
					PowerUP2.setBounds(594, 17, 79, 16);
					PowerUP2.setForeground(new java.awt.Color(255,0,0));
				}
				{
					PowerUP3 = new JLabel();
					PanelSecundario.add(PowerUP3);
					PowerUP3.setText("Bomba Extra.");
					PowerUP3.setBounds(445, 17, 108, 16);
					PowerUP3.setForeground(new java.awt.Color(255,0,0));
				}
				{
					PowerUP4 = new JLabel();
					PanelSecundario.add(PowerUP4);
					PowerUP4.setText("1+Disparo.");
					PowerUP4.setBounds(378, 17, 97, 16);
					PowerUP4.setForeground(new java.awt.Color(255,0,0));
				}
				{
					DisparoPower = new JLabel(powerarma);
					PanelSecundario.add(DisparoPower);
					DisparoPower.setBounds(397, 39, 23, 22);
				}
				{
					BombasPower = new JLabel(powerbomb);
					PanelSecundario.add(BombasPower);
					BombasPower.setBounds(470, 39, 19, 22);
				}
				{
					PowerVida = new JLabel(powervida);
					PanelSecundario.add(PowerVida);
					PowerVida.setBounds(548, 39, 23, 22);
				}
				{
					FullPower = new JLabel(powerfull);
					PanelSecundario.add(FullPower);
					FullPower.setBounds(606, 38, 23, 24);
				}
				
				{
					PuntosJugador = new JLabel();
					PanelSecundario.add(PuntosJugador);
					PuntosJugador.setText("Puntos: "+ContadorPuntaje);
					PuntosJugador.setBounds(654, 39, 195, 16);
					PuntosJugador.setForeground(new java.awt.Color(255,0,0));
					PuntosJugador.setFont(new java.awt.Font("Segoe UI",0,14));
				}
				{
					LabelVida = new JLabel();
					PanelSecundario.add(LabelVida);
					LabelVida.setText("Life Points: 500");
					LabelVida.setBounds(9, 6, 146, 16);
					LabelVida.setForeground(new java.awt.Color(255,0,0));
					LabelVida.setFont(new java.awt.Font("Segoe UI",0,14));
				}
				{
					BotonSalir = new JButton(botoncerrar);
					PanelSecundario.add(BotonSalir);
					BotonSalir.setBounds(767, 11, 27, 36);
					BotonSalir.setFont(new java.awt.Font("Segoe UI",0,14));
					BotonSalir.setForeground(new java.awt.Color(255,0,0));
					BotonSalir.setBackground(new java.awt.Color(0,0,0));
					BotonSalir.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
					BotonSalir.setFocusPainted(false);
					OyenteSalir oyente=new OyenteSalir();
					BotonSalir.addActionListener(oyente);
				}
				
				BotonSalir.setFocusable(false);
			}
		
			
			fondo1 = new JLabel(espacio);
			fondo1.setVisible(true);
			PanelPrincipal.add(fondo1);
			fondo1.setBounds(0, 0, 783, 600);
			
			fondo2 = new JLabel(espacio);
			fondo2.setVisible(true);
			PanelPrincipal.add(fondo2);
			fondo2.setBounds(770, 0, 26, 700);
			
			Fondo3 = new JLabel(espacio2);
			PanelSecundario.add(Fondo3);
			Fondo3.setBounds(2, 1, 793, 76);
			

			this.initgame();
			
		} catch (Exception e) {
			e.printStackTrace();
		}			
	}
	
	public NavesUsuario getUsuario(){
		return jugador;}
	
	public void initgame(){
		
		Nave1a.setVisible(false);
		Nave2a.setVisible(false);
		Nave3a.setVisible(false);
		Bomba1a.setVisible(false);
		Bomba2a.setVisible(false);
		Bomba3a.setVisible(false);
		PowerUP1.setVisible(false);
		PowerUP2.setVisible(false);
		PowerUP3.setVisible(false);
		PowerUP4.setVisible(false);
		DisparoPower.setVisible(false);
		BombasPower.setVisible(false);
		PowerVida.setVisible(false);
		FullPower.setVisible(false);
		
		LabelVida.setVisible(false);
		PuntosJugador.setVisible(false);

	}
	
	
	public void run() {
		Thread threadjugador=new Thread(jugador);
		threadjugador.start();
		mapa lvl=new mapa(this);
		lvl.start();
		inteligenciaenemigos.start();
		inteligenciabalas.start();
		inteligenciapowerup.start();
	}
	
	// METODOS QUE AGREGAN POWERUPS.
	public void insertPowerup(Point pos){
		PowerUpGenerico nuevopower=null;
			int randommov = (int) (Math.random()*4+1);
				if (randommov==1){nuevopower= new PowerBala(this);}		//POWER UP BALA.
				if (randommov==2){nuevopower= new PowerBomba(this);}	//POWER UP BOMBA.
				if (randommov==3){nuevopower= new PowerHp(this);}		//POWER UP HP.
				if (randommov==4){nuevopower= new PowerLife(this);}		//POWER UP LIFE.
		if (powerups.size()<4){
		powerups.addLast(nuevopower);
		nuevopower.setBounds(pos.x, pos.y, 30, 30);
		nuevopower.setLocation(pos);
		LabelNaves.add(nuevopower);
		
		}
	}
	
	public void retocarimagenes(){
		int vidas=0;
		int bombas=0;
		vidas=jugador.getVidasRestantes();
		bombas=jugador.getBombasRestantes();
		
		if (vidas==0){
			Nave1a.setVisible(false);
			Nave2a.setVisible(false);
			Nave3a.setVisible(false);
		}
		if (vidas==1){
			Nave1a.setVisible(true);
			Nave2a.setVisible(false);
			Nave3a.setVisible(false);
		}
		if (vidas==2){
			Nave1a.setVisible(true);
			Nave2a.setVisible(true);
			Nave3a.setVisible(false);
		}
		if (vidas==3){
			Nave1a.setVisible(true);
			Nave2a.setVisible(true);
			Nave3a.setVisible(true);
		}
		if (bombas==0){
			Bomba1a.setVisible(false);
			Bomba2a.setVisible(false);
			Bomba3a.setVisible(false);
		}
		if (bombas==1){
			Bomba1a.setVisible(true);
			Bomba2a.setVisible(false);
			Bomba3a.setVisible(false);
		}
		if (bombas==2){
			Bomba1a.setVisible(true);
			Bomba2a.setVisible(true);
			Bomba3a.setVisible(false);
		}
		if (bombas==3){
			Bomba1a.setVisible(true);
			Bomba2a.setVisible(true);
			Bomba3a.setVisible(true);
		}
		
	}
	public void actualizarporpower(){
		LabelVida.setText("Life Points: "+jugador.getVida());
		retocarimagenes();
		
		
	}
	public Iterable<Position<PowerUpGenerico>> getListaPowerUps(){
		return powerups.positions();
	}
	
	public void EliminarPowerUp(Position<PowerUpGenerico> p){
		LabelNaves.remove(p.element());
		try {
			powerups.remove(p);
		} catch (InvalidPositionException e) {}
	}
	
	
	// METODOS QUE AGREGAGAN DIFERENTES TIPOS DE NAVE.
	public Iterable<Position<NavesEnemiga>> getListaEnemigos(){
		return listadeenemigos.positions();
	}
	
	public void InsertarCaza(NaveCaza nc){
		listadeenemigos.addLast(nc);
		int x=(int)(Math.random()*1000+1)%this.getWidth();
		Point pos=new Point(x,-50);
		LabelNaves.add(nc);
		nc.setLocation(pos);
		this.repaint();
	}
	
	public void InsertarJefe(NaveBoss nd){
		listadeenemigos.addLast(nd);
		LabelNaves.add(nd);
		this.repaint();
	}
	
	public void InsertarDestructor(NaveDestructor nd){
		listadeenemigos.addLast(nd);
		LabelNaves.add(nd);
		this.repaint();
	}
	
	public void InsertarFederacion(NaveFederacion nf){
		listadeenemigos.addLast(nf);
		LabelNaves.add(nf);
		this.repaint();
	}
	
	public void SacarEnemigo(Position<NavesEnemiga> p){
		p.element().Explotar();
		ContadorPuntaje+=p.element().getPuntaje();
		PuntosJugador.setText("Puntos: "+ContadorPuntaje);
		try {listadeenemigos.remove(p);
		} catch (InvalidPositionException e) {}
	}
	
	
	
	//-------------------------------------------------------------
	//METODOS BALAS AMIGAS.
	
	public Iterable<Position<Bala>> getBalasAmigas(){
		return balasamigas.positions();
	}
	
	public void agregarBalaAmiga(Bala ba){
		this.LabelNaves.add(ba);
		balasamigas.addLast(ba);	
	}
	
	public void SacarBalaAmiga(Position<Bala> b){
		b.element().setIcon(null);
		b.element().setBounds(0, 0, 0, 0);
		LabelNaves.remove(b.element());
		try {
			balasamigas.remove(b);
		} catch (InvalidPositionException e) {}
	}
	
	public void usobomba(){
		for(Position<NavesEnemiga> e:this.getListaEnemigos()){
			if (!e.element().esjefe())SacarEnemigo(e);
		}
		for(Position<Bala> e:this.getBalasEnemigas()){
			SacarBalaEnemiga(e);
		}
		for(Position<Bala> e:this.getBalasAmigas()){
			SacarBalaAmiga(e);
		}
		repaint();
	}
	
	//-------------------------------------------------------------
	//METODOS BALAS ENEMIGAS.
	
	public Iterable<Position<Bala>> getBalasEnemigas(){
		return balasenemigas.positions();
	}
	
	public void agregarBalaEnemiga(Bala ba){
		this.LabelNaves.add(ba);
		balasenemigas.addLast(ba);
	}
	
	public void SacarBalaEnemiga(Position<Bala> b){
		LabelNaves.remove(b.element());
		LabelVida.setText("Life Points: "+jugador.getVida());
		try {
			balasenemigas.remove(b);
		} catch (InvalidPositionException e) {}
	}
	class OyenteSalir implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			System.exit(0);		
		}
	}
	
	class OyenteXwing implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			ejecutarcomoXwing();
		}
	}	
	
	class OyenteHalcon implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			ejecutarcomoHalcon();
		}
	}	
	
	public void ejecutarcomoXwing(){
		Nave1a.setVisible(true);
		Nave2a.setVisible(true);
		Nave3a.setVisible(true);
		Bomba1a.setVisible(true);
		Bomba2a.setVisible(true);
		Bomba3a.setVisible(true);
		PowerUP1.setVisible(true);
		PowerUP2.setVisible(true);
		PowerUP3.setVisible(true);
		PowerUP4.setVisible(true);
		DisparoPower.setVisible(true);
		BombasPower.setVisible(true);
		PowerVida.setVisible(true);
		FullPower.setVisible(true);

		LabelVida.setVisible(true);
		PuntosJugador.setVisible(true);
		
		BotonNaveHalcon.setVisible(false);
		BotonNaveXwing.setVisible(false);
		TextoNaveXwing.setVisible(false);
		TextoNaveHalcon.setVisible(false);
		
		jugador= new NaveXwing(this,navexwing);
		jugador.setBounds(400, 450, 40, 40);
		LabelNaves.add(jugador);

		ImagenchiquitaHalcon.setVisible(false);
		ImagenchiquitaXwing.setVisible(false);
		
		listadeenemigos = new DoubleLinkedList<NavesEnemiga>();
		powerups=new DoubleLinkedList<PowerUpGenerico>();
		balasamigas=new DoubleLinkedList<Bala>();
		balasenemigas=new DoubleLinkedList<Bala>();
		inteligenciaenemigos=new enemigosAI(this);
		inteligenciabalas=new balasAI(this);
		inteligenciapowerup=new powerAI(this);
		
		Nave1a.setIcon(navexwing);
		Nave2a.setIcon(navexwing);
		Nave3a.setIcon(navexwing);
		
		navesvidasimagenes=navexwing;
		
		this.run();
	}
	
	public void ejecutarcomoHalcon(){
		Nave1a.setVisible(true);
		Nave2a.setVisible(true);
		Nave3a.setVisible(true);
		Bomba1a.setVisible(true);
		Bomba2a.setVisible(true);
		Bomba3a.setVisible(true);
		PowerUP1.setVisible(true);
		PowerUP2.setVisible(true);
		PowerUP3.setVisible(true);
		PowerUP4.setVisible(true);
		DisparoPower.setVisible(true);
		BombasPower.setVisible(true);
		PowerVida.setVisible(true);
		FullPower.setVisible(true);

		LabelVida.setVisible(true);
		PuntosJugador.setVisible(true);
		
		BotonNaveHalcon.setVisible(false);
		BotonNaveXwing.setVisible(false);
		TextoNaveXwing.setVisible(false);
		TextoNaveHalcon.setVisible(false);
		
		ImagenchiquitaHalcon.setVisible(false);
		ImagenchiquitaXwing.setVisible(false);
		
		
		jugador= new NaveHalcon(this,navehalcon);
		jugador.setBounds(400, 450, 40, 40);
		LabelNaves.add(jugador);

		
		listadeenemigos = new DoubleLinkedList<NavesEnemiga>();
		powerups=new DoubleLinkedList<PowerUpGenerico>();
		balasamigas=new DoubleLinkedList<Bala>();
		balasenemigas=new DoubleLinkedList<Bala>();
		inteligenciaenemigos=new enemigosAI(this);
		inteligenciabalas=new balasAI(this);
		inteligenciapowerup=new powerAI(this);
		Nave1a.setIcon(navehalcon);
		Nave2a.setIcon(navehalcon);
		Nave3a.setIcon(navehalcon);
		
		this.run();
	}
	
	//------------------------------------------------------------
	//METODOS FINALES PARA EL JUEGO.
	
	public void ganojuego(){
		
		int total=ContadorPuntaje + 3000;
		PanelPrincipal.setVisible(false);
		PanelPrincipal = new JPanel();
		
		getContentPane().add(PanelPrincipal);
		getContentPane().remove(PanelSecundario);
		
		PanelPrincipal.setLayout(null);
		PanelPrincipal.setBounds(0, 0, 800, 600);
		PanelPrincipal.setBackground(new java.awt.Color(0,0,0));
		PanelPrincipal.setVisible(true);

		PuntosJugador.setText("FELICITACIONES, HA GANADO! Puntos totales: "+total+"  Record: 15243657 ");
		PuntosJugador.setBounds(0, 10, 800, 25);
		PuntosJugador.setForeground(new java.awt.Color(255,0,0));
		PuntosJugador.setFont(new java.awt.Font("Segoe UI",0,25));	

		PanelPrincipal.add(PuntosJugador);
		PanelPrincipal.add(BotonSalir);
		guardarPuntaje(total);
	}
	public void perdiojuego(){
		PanelPrincipal.setVisible(false);
		PanelPrincipal = new JPanel();
		getContentPane().add(PanelPrincipal);
		getContentPane().remove(PanelSecundario);
		PanelPrincipal.setLayout(null);
		PanelPrincipal.setBounds(0, 0, 800, 600);
		PanelPrincipal.setBackground(new java.awt.Color(0,0,0));
		PanelPrincipal.setVisible(true);
		PuntosJugador.setText("GAME OVER Puntos totales: "+ContadorPuntaje+"  Record: 15243657 ");
		PuntosJugador.setBounds(120, 10, 800, 25);
		PuntosJugador.setForeground(new java.awt.Color(255,0,0));
		PuntosJugador.setFont(new java.awt.Font("Segoe UI",0,25));	
		PanelPrincipal.add(PuntosJugador);
		PanelPrincipal.add(BotonSalir);
		guardarPuntaje(ContadorPuntaje);

	}
	private void guardarPuntaje(int t){
		final JFrame parent = new JFrame();
        parent.setVisible(true);
        playerName = JOptionPane.showInputDialog(parent,"Ingrese su Nombre", null);		
        if (playerName=="")
        	playerName="Player";
        
        JLabel ranking=new JLabel();
		ranking.setBounds(300,180,200,30);
		JLabel ranking1=new JLabel();
		ranking1.setBounds(300,210,200,30);
		JLabel ranking2=new JLabel();
		ranking2.setBounds(300,240,200,30);
		JLabel ranking3=new JLabel();
		ranking3.setBounds(300,270,200,30);
		JLabel ranking4=new JLabel();
		ranking4.setBounds(300,300,200,30);
		JLabel ranking5=new JLabel();
		ranking5.setBounds(300,330,200,30);
		
		PanelPrincipal.add(ranking);
		PanelPrincipal.add(ranking1);
		PanelPrincipal.add(ranking2);
		PanelPrincipal.add(ranking3);
		PanelPrincipal.add(ranking4);
		PanelPrincipal.add(ranking5);
		PanelPrincipal.repaint();
		String[] p=new String[5];
		File f=new File("ranking.txt");
		Escritor e;
		
		if(f.exists()){
		Lector l=new Lector(f.getAbsolutePath());
		String s=l.leerLinea();
		int j=0;
		while((j<5) && (s!=null)){
			if(j==0)
				p[0]=s;
			if(j==1)
				p[1]=s;
			if(j==2)
				p[2]=s;
			if(j==3)
				p[3]=s;
			if(j==4)
				p[4]=s;
			

			s=l.leerLinea();
			j++;
		}
		
		String delimitadores= "[ ]";
		j=0;
		
		while(j<5){
			if(p[j]!=null){
				String[] jug=p[j].split(delimitadores);
				int aux=Integer.parseInt(jug[1]);
				if(aux<t){
					String aux2=p[j];
					p[j]=playerName+" "+t;
					j++;
					while(j<5){
						String aux3=p[j];
						p[j]=aux2;
						aux2=aux3;
						j++;
					}
				}
				else
					j++;
			}
		}
		l.cerrarArchivo();
		}
	f.delete();
	f=new File("ranking.txt");
	e=new Escritor(f.getAbsolutePath());
	if(p[0]==null){
		p[0]=new String(playerName + " "+t+"\n");
		}
	if(p[0]!=null)
	e.escribir(p[0]+"\n");
	if(p[1]!=null)
	e.escribir(p[1]+"\n");
	if(p[2]!=null)
	e.escribir(p[2]+"\n");	
	if(p[3]!=null)
	e.escribir(p[3]+"\n");	
	if(p[4]!=null)
	e.escribir(p[4]+"\n");	
	e.cerrarArchivo();
	
	ranking.setText("Top 5:");
	ranking1.setText(p[0]);
	ranking2.setText(p[1]);
	ranking3.setText(p[2]);
	ranking4.setText(p[3]);
	ranking5.setText(p[4]);	
	}
}
